﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System . ServiceModel ;
using Teenusekiht ;
using Teenusekiht . EnumIds ;

    public class CommServ : IDisposable
    {

        public byte Sets {get;set;}

        public bool Error {get;set;}
        private IService1 serv = null ;

        public CommServ ()
    	{
            Error = false ;
        }

        public CommServ ( byte sets )
    	{
            this . Sets = sets ;
            Error = false ;
        }

        public void Open ()
        {
            try {
                if ( serv == null ) {
                    serv = new ChannelFactory < IService1 > ( "Serv" ) . CreateChannel () ; } }
              catch ( Exception ex ) {
                HandleEx ( ex ) ; }
        }

        public int? GetCount ( int? kId )
        {
            int? result = null ;
            try {
                Open () ;
                if ( serv != null ) {
                    switch ( Sets ) {
                      case SETS . CAFECA : case SETS . CAFECM : case SETS . CAFECU :
                        result = serv . KohvikuKommentaarideArv ( ( int ) kId , Rakendus . RAKENDUSE_KOOD ) ; break ;
                      case SETS . FOODCA : case SETS . FOODCM : case SETS . FOODCU :
                        break ;
                      default :
                        result = null ; break ; } } }
              catch ( Exception ex ) {
                HandleEx ( ex ) ; }
            return ( result ) ;
        }

        public GenCom [] GetComms ( int? kId , int nPage , int cComm )
        {
            GenCom [] result = null ;
            try {
                Open () ;
                if ( serv != null ) {
                    switch ( Sets ) {
                      case SETS . CAFECA : case SETS . CAFECM : case SETS . CAFECU :
                        result = serv . AnnaKohvikuKommentaarid ( ( int ) kId , cComm * ( nPage - 1 ) , cComm , Rakendus . RAKENDUSE_KOOD ) ; break ;
                      case SETS . FOODCA : case SETS . FOODCM : case SETS . FOODCU :
                        break ;
                      default :
                        result = null ; break ; } } }
              catch ( Exception ex ) {
                HandleEx ( ex ) ; }
            return ( result ) ;
        }

        public GenCom SetComm ( EdiCom data )
        {
            GenCom result = null ;
            try {
                Open () ;
                if ( serv != null ) {
                    switch ( Sets ) {
                      case SETS . CAFECA : case SETS . CAFECM : case SETS . CAFECU :
                        result = serv . LisaKohvikuKommentaar ( data , Rakendus . RAKENDUSE_KOOD ) ; break ;
                      case SETS . FOODCA : case SETS . FOODCM : case SETS . FOODCU :
                        break ;
                      default :
                        result = null ; break ; } } }
              catch ( Exception ex ) {
                HandleEx ( ex ) ; }
            return ( result ) ;
        }

        public GenCom ModiComm ( int? uId , int? cId , string text , ushort rate )
        {
            if ( ( uId != null ) && ( cId != null ) ) {}//else false
            GenCom result = null ;
            EdiCom data = new EdiCom () ;
            data . cId = cId ;
            data . cUId = uId ;
            data . dText = text ;
            data . dValue = rate ;
            try {
                Open () ;
                if ( serv != null ) {
                    switch ( Sets ) {
                      case SETS . CAFECA : case SETS . CAFECM : case SETS . CAFECU :
                        result = serv . MuudaKohvikuKommentaar ( data , Rakendus . RAKENDUSE_KOOD ) ; break ;
                      case SETS . FOODCA : case SETS . FOODCM : case SETS . FOODCU :
                        break ;
                      default :
                        result = null ; break ; } } }
              catch ( Exception ex ) {
                HandleEx ( ex ) ; }
            return ( result ) ;
        }

        public bool RepoComm ( int? uId , int? cId )
        {
            if ( ( uId != null ) && ( cId != null ) ) {}//else false
            StaCom data = new StaCom () ;
            data . cId = cId ;
            data . cState = CSTA . RAPORTEERITUD ;
            bool result = false ;
            try {
                Open () ;
                if ( serv != null ) {
                    switch ( Sets ) {
                      case SETS . CAFECA : case SETS . CAFECM : case SETS . CAFECU :
                        result = serv . MuudaKohvikuKommentaariOlek ( data , Rakendus . RAKENDUSE_KOOD ) ; break ;
                      case SETS . FOODCA : case SETS . FOODCM : case SETS . FOODCU :
                        break ;
                      default :
                        result = false ; break ; } } }
              catch ( Exception ex ) {
                HandleEx ( ex ) ; }
            return ( result ) ;
        }

        public bool DeleComm ( int? uId , int? cId )
        {
            return false ;
        }

        public bool RestComm ( int? uId , int? cId )
        {
            return false ;
        }

        public bool HideComm ( int? uId , int? cId )
        {
            StaCom data = new StaCom () ;
            data . cId = cId ;
            data . cState = 1 ;
            bool result = false ;
            try {
                Open () ;
                if ( serv != null ) {
                    switch ( Sets ) {
                      case SETS . CAFECA : case SETS . CAFECM : case SETS . CAFECU :
                        result = serv . MuudaKohvikuKommentaariPeidetus ( data , Rakendus . RAKENDUSE_KOOD ) ; break ;
                      case SETS . FOODCA : case SETS . FOODCM : case SETS . FOODCU :
                        break ;
                      default :
                        result = false ; break ; } } }
              catch ( Exception ex ) {
                HandleEx ( ex ) ; }
            return ( result ) ;
        }

        public bool ShowComm ( int? uId , int? cId )
        {
            StaCom data = new StaCom () ;
            data . cId = cId ;
            data . cState = 0 ;
            bool result = false ;
            try {
                Open () ;
                if ( serv != null ) {
                    switch ( Sets ) {
                      case SETS . CAFECA : case SETS . CAFECM : case SETS . CAFECU :
                        result = serv . MuudaKohvikuKommentaariPeidetus ( data , Rakendus . RAKENDUSE_KOOD ) ; break ;
                      case SETS . FOODCA : case SETS . FOODCM : case SETS . FOODCU :
                        break ;
                      default :
                        result = false ; break ; } } }
              catch ( Exception ex ) {
                HandleEx ( ex ) ; }
            return ( result ) ;
        }

        public bool RemoComm ( int? uId , int? cId )
        {
            return false ;
        }

        public bool AlloComm ( int? uId , int? cId )
        {
            return false ;
        }

        public bool DenyComm ( int? uId , int? cId )
        {
            return false ;
        }

        public RatCom [] RetrRates ()
        {
            RatCom [] result = null ;
            try {
                Open () ;
                if ( serv != null ) {
                    switch ( Sets ) {
                      case SETS . CAFECA : case SETS . CAFECM : case SETS . CAFECU :
                        result = serv . AnnaKohvikuKommentaarideHinded ( Rakendus . RAKENDUSE_KOOD ) ; break ;
                      case SETS . FOODCA : case SETS . FOODCM : case SETS . FOODCU :
                        break ;
                      default :
                        result = null ; break ; } } }
              catch ( Exception ex ) {
                HandleEx ( ex ) ; }
            return ( result ) ;
        }

        public void Close ()
        {
            try {
                if ( serv != null ) {
                    serv . Dispose () ;
                    serv = null ; } }
              catch ( Exception ex ) {
                HandleEx ( ex ) ; }
        }

        public void HandleEx ( Exception ex )
        {
            Error = true ;
            try { serv . Dispose () ; }
              catch ( Exception ) {}
              finally { serv = null ; }
        }

        public void Dispose ()
        {
            Close () ;
        }

    }
